package com.crane.app.web.controller;


import com.crane.common.core.dto.MultiResponse;
import com.crane.common.core.dto.PageQuery;
import com.crane.common.core.dto.SingleResponse;
import com.crane.common.core.utils.BaseContextHandler;
import com.crane.common.core.utils.CollectionUtil;
import com.crane.common.core.utils.StringUtil;
import com.crane.common.framework.utils.BeanUtil;
import com.crane.oss.app.bean.req.AppQueryStudentClassCourseReq;
import com.crane.oss.app.bean.req.AppQueryTeacherClassCourseReq;
import com.crane.oss.app.bean.resp.*;
import com.crane.oss.app.service.AppUserService;
import com.crane.oss.school.bean.resp.StudentResp;
import com.crane.oss.school.bean.resp.TeacherResp;
import com.crane.oss.school.dao.ClassesInfoDao;
import com.crane.oss.school.model.ClassesInfo;
import com.crane.oss.school.service.StudentService;
import com.crane.oss.school.service.TeacherService;
import com.crane.oss.sports.XunFeiAppApiService;
import com.crane.oss.sports.XunFeiClassCourseAnalysisApiService;
import com.crane.oss.sports.bean.resquest.*;
import com.crane.oss.xunfei.req.*;
import com.crane.oss.xunfei.resp.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(value = "AppTeachCourseController", tags = {"课堂记录"})
@RestController
@RequestMapping("/app/course")
public class AppCourseController extends BaseAppController {

    @Resource
    AppUserService appUserService;

    @Resource
    TeacherService teacherService;

    @Resource
    StudentService studentService;

    @Resource
    ClassesInfoDao classesInfoDao;

    @Resource
    XunFeiAppApiService xunFeiAppApiService;

    @Resource
    XunFeiClassCourseAnalysisApiService xunFeiClassCourseAnalysisApiService;


    @ApiOperation(value = "学生课堂列表", notes = "学生课堂列表")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "AppQueryStudentClassCourseReq")
    @PostMapping("/studentCourse")
    public MultiResponse<AppAnalysisStudentClassCourseResp> getStudentClassCourse(@RequestBody AppQueryStudentClassCourseReq request) {
        Long userId=BaseContextHandler.getUserId();
        StudentResp studentResp=appUserService.getDefaultStudent(userId);
        if(null == studentResp){
            return MultiResponse.buildFailure("未绑定学生");
        }
        QueryAppAnalysisStudentClassCourseReq queryAppAnalysisStudentClassCourseReq=new QueryAppAnalysisStudentClassCourseReq();
        queryAppAnalysisStudentClassCourseReq.setStudentId(studentResp.getStudentId().toString());
        queryAppAnalysisStudentClassCourseReq.setBeginTime(request.getBeginTime().getTime());
        queryAppAnalysisStudentClassCourseReq.setEndTime(request.getEndTime().getTime());
        queryAppAnalysisStudentClassCourseReq.setPage(request.getPageNo());
        queryAppAnalysisStudentClassCourseReq.setLimit(request.getPageSize());
        ApiListResponse<AppAnalysisStudentClassCourseResp> response=xunFeiAppApiService.getStudentClassCourse(queryAppAnalysisStudentClassCourseReq,studentResp.getPartyId().toString(),null,userId.toString());
        if(null != response && response.getMsg().equals("success")){
            return MultiResponse.of(response.getData(),response.getTotal(),request);
        }
        return MultiResponse.buildFailure("请求失败");
    }

    @ApiOperation(value = "老师课堂列表", notes = "老师课堂列表")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "AppQueryTeacherClassCourseReq")
    @PostMapping("/teacherCourse")
    public MultiResponse<AppAnalysisStudentClassCourseResp> getTeacherClassCourse(@RequestBody AppQueryTeacherClassCourseReq request) {
        Long userId=BaseContextHandler.getUserId();
        TeacherResp teacherResp=teacherService.queryTeacherByAppUserId(userId);
        QueryAppAnalysisTeacherClassCourseReq queryAppAnalysisTeacherClassCourseReq=new QueryAppAnalysisTeacherClassCourseReq();
        queryAppAnalysisTeacherClassCourseReq.setBeginTime(request.getBeginTime().getTime());
        queryAppAnalysisTeacherClassCourseReq.setEndTime(request.getEndTime().getTime());
        queryAppAnalysisTeacherClassCourseReq.setPage(request.getPageNo());
        queryAppAnalysisTeacherClassCourseReq.setLimit(request.getPageSize());
        queryAppAnalysisTeacherClassCourseReq.setTeacherId(teacherResp.getTeacherId().toString());
        queryAppAnalysisTeacherClassCourseReq.setFlag(1);
        if(null != request.getClassId()){
            queryAppAnalysisTeacherClassCourseReq.setClassId(request.getClassId().toString());
        }
        ApiListResponse<AppAnalysisStudentClassCourseResp> response=xunFeiAppApiService.getTeacherClassCourse(queryAppAnalysisTeacherClassCourseReq,teacherResp.getPartyId().toString(),null,userId.toString());
        if(null != response && response.getMsg().equals("success")){
            return MultiResponse.of(response.getData(),response.getTotal(),request);
        }
        return MultiResponse.buildFailure("请求失败");
    }


    @ApiOperation(value = "学生课堂风采", notes = "学生课堂风采")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "PageQuery")
    @PostMapping("/studentPicture")
    public MultiResponse getTeacherClassCourse(@RequestBody PageQuery request) {
        Long userId=BaseContextHandler.getUserId();
        StudentResp studentResp=appUserService.getDefaultStudent(userId);
        if(null == studentResp){
            return MultiResponse.buildFailure("未绑定学生");
        }
        QueryAppAnalysisStudentPictureReq queryAppAnalysisStudentPictureReq=new QueryAppAnalysisStudentPictureReq();
        queryAppAnalysisStudentPictureReq.setStudentId(studentResp.getStudentId().toString());
        queryAppAnalysisStudentPictureReq.setPage(request.getPageNo());
        queryAppAnalysisStudentPictureReq.setLimit(request.getPageSize());
        ApiListResponse<AppAnalysisStudentPictureResp> response=xunFeiAppApiService.getStudentPicture(queryAppAnalysisStudentPictureReq,studentResp.getPartyId().toString(),null,userId.toString());
        if(null != response && response.getMsg().equals("success") && CollectionUtil.isNotEmpty(response.getData())){
           Map<String,List<AppAnalysisStudentPictureResp>> group=response.getData().stream().collect(Collectors.groupingBy(AppAnalysisStudentPictureResp ::getCreateDate));
           List<Object> result=new ArrayList<>();
           for(Map.Entry<String, List<AppAnalysisStudentPictureResp>> enetry : group.entrySet()){
               Map<String,Object> map=new HashMap<>();
               map.put("date",enetry.getKey());
               map.put("list",enetry.getValue());
               result.add(map);
           }
            return MultiResponse.of(result,response.getTotal(),request);
        }
        return MultiResponse.of(new ArrayList<>(),0,request);
    }


    @ApiOperation(value = "老师-班级课堂记录", notes = "老师-班级课堂记录")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryCourseAndClassIdAnalyzeReq")
    @PostMapping("/record/info")
    public SingleResponse<AppClassCourseIdAnalysisInfoResp> recordInfo(@RequestBody QueryCourseAndClassIdAnalyzeReq request) {
        AppClassCourseIdAnalysisInfoResp resp= new AppClassCourseIdAnalysisInfoResp();
        Long userId=BaseContextHandler.getUserId();
        TeacherResp teacherResp=teacherService.queryTeacherByAppUserId(userId);
        QueryAnalysisClassCourseIdReq queryAnalysisClassCourseIdReq =new QueryAnalysisClassCourseIdReq();
        queryAnalysisClassCourseIdReq.setCourseId(request.getCourseId());
        ApiSingleResponse<ClassCourseIdIndexAnalysisResp> response=xunFeiClassCourseAnalysisApiService.getAnalysisClassCourseIndex(queryAnalysisClassCourseIdReq,teacherResp.getPartyId().toString(), null,userId.toString());
        if(response.getMsg().equals("success") && null != response.getData()){
            BeanUtil.copyProperties(response.getData(),resp);
            ClassesInfo classesInfo = classesInfoDao.selectById(Long.valueOf(request.getClassId()));
            if (null != classesInfo) {
                resp.setGrade(classesInfo.getGrade());
            }
            return SingleResponse.of(resp);
        }
        return SingleResponse.of(resp);
    }


    @ApiOperation(value = "老师-课堂记录学生列表", notes = "老师-课堂记录学生列表")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryStudentAnalyzeReq")
    @PostMapping("/student/list")
    public MultiResponse<AppStudentAnalyzeResp> courseStudentAnalyze(@RequestBody QueryStudentAnalyzeReq request) {
        Long userId=BaseContextHandler.getUserId();
        TeacherResp teacherResp=teacherService.queryTeacherByAppUserId(userId);
        QueryAnalysisClassCourseStudentListReq queryAnalysisClassCourseIdReq =new QueryAnalysisClassCourseStudentListReq();
        queryAnalysisClassCourseIdReq.setCourseId(request.getCourseId());
        if(!StringUtil.isEmpty(request.getXsxb())){
            queryAnalysisClassCourseIdReq.setGender(request.getXsxb());
        }
        queryAnalysisClassCourseIdReq.setPage(request.getPageNo());
        queryAnalysisClassCourseIdReq.setLimit(request.getPageSize());
        ApiListResponse<ClassCourseAnalysisStudentListResp> response=xunFeiClassCourseAnalysisApiService.getAnalysisClassCourseStudentList(queryAnalysisClassCourseIdReq,teacherResp.getPartyId().toString(), null,userId.toString());
        if(null != response  && response.getMsg().equals("success")){
            List<AppStudentAnalyzeResp> list=BeanUtil.copyList(response.getData(),AppStudentAnalyzeResp.class);
            return MultiResponse.of(list,response.getTotal(),request);
        }
        return MultiResponse.buildFailure("错误请求");
    }

    @ApiOperation(value = "老师-学生个人详情", notes = "老师-学生个人详情")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryOneStudentAnalyzeReq")
    @PostMapping("/teacherStudent")
    public SingleResponse<AppOneStudentAnalyzeResp> courseTeacherStudentAnalyze (@RequestBody QueryOneStudentAnalyzeReq request) {
        Long userId=BaseContextHandler.getUserId();
        TeacherResp teacherResp=teacherService.queryTeacherByAppUserId(userId);
        QueryAnalysisClassCourseStudentReq queryAnalysisClassCourseIdReq =new QueryAnalysisClassCourseStudentReq();
        queryAnalysisClassCourseIdReq.setCourseId(request.getCourseId());
        queryAnalysisClassCourseIdReq.setStudentId(request.getStudentId());
        ApiSingleResponse<ClassCourseAnalysisStudentInfoResp> response=xunFeiClassCourseAnalysisApiService.getAnalysisClassCourseStudentInfo(queryAnalysisClassCourseIdReq,teacherResp.getPartyId().toString(), null,userId.toString());
        if(null!=response && response.getMsg().equals("success")){
            AppOneStudentAnalyzeResp resp=new AppOneStudentAnalyzeResp();
            BeanUtil.copyProperties(response.getData(),resp);
            return SingleResponse.of(resp);

        }
        return SingleResponse.buildFailure("请求失败");
    }


    @ApiOperation(value = "家长-学生个人详情", notes = "家长-学生个人详情")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryOneStudentAnalyzeReq")
    @PostMapping("/student")
    public SingleResponse<AppOneStudentAnalyzeResp> courseOneStudentAnalyze (@RequestBody QueryOneStudentAnalyzeReq request) {
        Long userId=BaseContextHandler.getUserId();
        Long partId= null;

        QueryAnalysisClassCourseStudentReq queryAnalysisClassCourseIdReq =new QueryAnalysisClassCourseStudentReq();
        queryAnalysisClassCourseIdReq.setCourseId(request.getCourseId());
        if(StringUtil.isEmpty(request.getStudentId())){
            StudentResp studentResp=appUserService.getDefaultStudent(userId);
            partId=studentResp.getPartyId();
            if(null == studentResp){
                return SingleResponse.buildFailure("未绑定学生");
            }
            queryAnalysisClassCourseIdReq.setStudentId(studentResp.getStudentId().toString());
        }else {
            queryAnalysisClassCourseIdReq.setStudentId(request.getStudentId());
            AppUserStudentInfoResp userInfoResp = appUserService.queryStudentInfo(userId);
            if(CollectionUtil.isNotEmpty(userInfoResp.getList())){
                for(AppStudentInfo appStudentInfo: userInfoResp.getList()){
                    if(appStudentInfo.getStudentId().toString().equals(request.getStudentId())){
                        partId=appStudentInfo.getPartyId();
                    }
                }
            }
        }
        if(null == partId){
            return SingleResponse.buildFailure("未绑定学生");
        }

        ApiSingleResponse<ClassCourseAnalysisStudentInfoResp> response=xunFeiClassCourseAnalysisApiService.getAnalysisClassCourseStudentInfo(queryAnalysisClassCourseIdReq,partId.toString(), null,userId.toString());
        if(null!=response && response.getMsg().equals("success")){
            AppOneStudentAnalyzeResp resp=new AppOneStudentAnalyzeResp();
            BeanUtil.copyProperties(response.getData(),resp);
            return SingleResponse.of(resp);

        }
        return SingleResponse.buildFailure("请求失败");
    }


    @ApiOperation(value = "家长班级参数标准", notes = "课堂记录-班级参数标准")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryCourseStandardReq")
    @PostMapping("/standard")
    public MultiResponse<ClassCourseAnalysisStandardResp> courseStandard(@RequestBody QueryCourseStandardReq request) {
        Long userId=BaseContextHandler.getUserId();
        StudentResp studentResp=appUserService.getDefaultStudent(userId);
        if(null == studentResp){
            return MultiResponse.buildFailure("未绑定学生");
        }
        QueryAnalysisClassCourseStandardReq req =new QueryAnalysisClassCourseStandardReq();
        req.setClassId(request.getClassId());
        req.setClassType(request.getClassType());
        ApiListResponse<ClassCourseAnalysisStandardResp> response=xunFeiClassCourseAnalysisApiService.getAnalysisCourseStandard(req,studentResp.getPartyId().toString(), null,userId.toString());
        if(null != response  && response.getMsg().equals("success")){
            return MultiResponse.ofWithoutTotal(response.getData());
        }
        return MultiResponse.buildFailure("错误请求");
    }




    @ApiOperation(value = "家长-学生个人运动处方", notes = "家长-学生个人运动处方")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryOneStudentCycleAnalyzeReq")
    @PostMapping("/prescription")
    public SingleResponse<AppStudentPrescriptionReportResp> getStudentPrescription (@RequestBody QueryOneStudentCycleAnalyzeReq request) {
        Long userId=BaseContextHandler.getUserId();
        StudentResp studentResp=appUserService.getDefaultStudent(userId);
        if(null == studentResp){
            return SingleResponse.buildFailure("未绑定学生");
        }
        QueryAppStudentPhysicalReportReq queryAppStudentPhysicalReportReq =new QueryAppStudentPhysicalReportReq();
        queryAppStudentPhysicalReportReq.setSportCycleId(request.getCycleId());
        queryAppStudentPhysicalReportReq.setStudentId(studentResp.getStudentId().toString());
        ApiSingleResponse<AppStudentPrescriptionReportResp> response=xunFeiAppApiService.getAppStudentPrescription(queryAppStudentPhysicalReportReq,studentResp.getPartyId().toString(), null,userId.toString());
        if(null!=response && response.getMsg().equals("success")){
            return SingleResponse.of(response.getData());

        }
        return SingleResponse.buildFailure("请求失败");
    }

    @ApiOperation(value = "老师-学生个人运动处方", notes = "老师-学生个人运动处方")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryOneStudentCycleAnalyzeReq")
    @PostMapping("/teacherPrescription")
    public SingleResponse<AppStudentPrescriptionReportResp> getTeacherStudentPrescription (@RequestBody QueryOneStudentCycleAnalyzeReq request) {
        Long userId=BaseContextHandler.getUserId();
        TeacherResp teacherResp=teacherService.queryTeacherByAppUserId(userId);
        QueryAppStudentPhysicalReportReq queryAppStudentPhysicalReportReq =new QueryAppStudentPhysicalReportReq();
        queryAppStudentPhysicalReportReq.setSportCycleId(request.getCycleId());
        queryAppStudentPhysicalReportReq.setStudentId(request.getStudentId());
        ApiSingleResponse<AppStudentPrescriptionReportResp> response=xunFeiAppApiService.getAppStudentPrescription(queryAppStudentPhysicalReportReq,teacherResp.getPartyId().toString(), null,userId.toString());
        if(null!=response && response.getMsg().equals("success")){
            StudentResp studentResp=studentService.queryStudent(Long.valueOf(request.getStudentId()));
            if(null !=response.getData()){
                response.getData().setImg(studentResp.getImg());
            }
            return SingleResponse.of(response.getData());

        }
        return SingleResponse.buildFailure("请求失败");
    }

    @ApiOperation(value = "家长-学生个人运动处方详情", notes = "家长-学生个人运动处方详情")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryStudentPhysicalReportDetailReq")
    @PostMapping("/prescriptionDetail")
    public SingleResponse<String> getStudentPrescriptionDetail (@RequestBody QueryStudentPhysicalReportDetailReq request) {
        Long userId=BaseContextHandler.getUserId();
        StudentResp studentResp=appUserService.getDefaultStudent(userId);
        if(null == studentResp){
            return SingleResponse.buildFailure("未绑定学生");
        }
        QueryAppStudentPhysicalReportDetailReq queryAppStudentPhysicalReportDetailReq =new QueryAppStudentPhysicalReportDetailReq();
        queryAppStudentPhysicalReportDetailReq.setPrescriptionId(request.getPrescriptionId());
        ApiSingleResponse<String> response=xunFeiAppApiService.getAppStudentPrescriptionDetail(queryAppStudentPhysicalReportDetailReq,studentResp.getPartyId().toString(), null,userId.toString());
        if(null!=response && response.getMsg().equals("success")){
            return SingleResponse.of(response.getData());

        }
        return SingleResponse.buildFailure("请求失败");
    }

    @ApiOperation(value = "老师-学生个人运动处方详情", notes = "老师-学生个人运动处方详情")
    @ApiImplicitParam(name = "request", value = "请求对象", required = true, dataType = "QueryStudentPhysicalReportDetailReq")
    @PostMapping("/teacherPrescriptionDetail")
    public SingleResponse<String> getTeacherStudentPrescriptionDetail (@RequestBody QueryStudentPhysicalReportDetailReq request) {
        Long userId=BaseContextHandler.getUserId();
        TeacherResp teacherResp=teacherService.queryTeacherByAppUserId(userId);
        QueryAppStudentPhysicalReportDetailReq queryAppStudentPhysicalReportDetailReq =new QueryAppStudentPhysicalReportDetailReq();
        queryAppStudentPhysicalReportDetailReq.setPrescriptionId(request.getPrescriptionId());
        ApiSingleResponse<String> response=xunFeiAppApiService.getAppStudentPrescriptionDetail(queryAppStudentPhysicalReportDetailReq,teacherResp.getPartyId().toString(), null,userId.toString());
        if(null!=response && response.getMsg().equals("success")){
            return SingleResponse.of(response.getData());

        }
        return SingleResponse.buildFailure("请求失败");
    }

}
